#include "rand.h"

#define ROT(x,k) (((x)<<(k))|((x)>>(32-(k))))

extern uint32_t randval(struct randctx *x)
{
    uint32_t e = x->a - ROT(x->b, 27);
    x->a = x->b ^ ROT(x->c, 17);
    x->b = x->c + x->d;
    x->c = x->d + e;
    x->d = e + x->a;
    return x->d;
}

extern void randinit(struct randctx *x, uint32_t seed)
{
    uint32_t i;
    x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
    for (i=0; i<20; ++i) {
        (void)randval(x);
    }
}
